package com.fireline.beauty.product;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.bean.CpkcVo;
import com.fireline.beauty.bean.FwxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;

/**
 * 产品库存改变对话框 ：产品入出库
 * */
public class CpkcDialog extends Dialog {

	protected int result = SWT.CANCEL;
	protected Shell shell;
	private CLabel labCbj;
	private CLabel labLb;
	private CLabel labCpmc;
	// ========================
	// 业务数据
	// ===========================
	public FwxmVo fwxm;
	private String xmbh;

	private Spinner textSl;
	private Text textBz;
	private Text textdj;
	/**
	 * @wbp.nonvisual location=47,542
	 */
	private final Dataset cpxxSet = new Dataset(
			"select * ,\r\n(kcsl*cbj) kczj --存库总价\r\n from cpxx \r\nwhere \r\njy<>1 and\r\n? is null  or cpbh =? or cpmc like '%'||?||'%'  or jm like '%'||?||'%'  \r\n",
			SqliteJdbcImpl.INSTANCE);
	private CLabel labCpbh;
	private CLabel labCpgg;

	public CpkcVo kcvo;
	private int lb = 1; // 1 入库 2 出库

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 * @param cpxxSet
	 *            产品基本信息
	 * @param lb
	 *            库存改变类型 1 入库 2 出库
	 */
	public CpkcDialog(Shell parent, int style, Dataset cpxxSet, int lb) {
		super(parent, style);
		this.cpxxSet.populate(cpxxSet, cpxxSet.getSelectedIndex());
		this.cpxxSet.setSelected(0);
		this.lb = lb;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		Display display = getParent().getDisplay();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		String lbmc = lb == 1 ? "入库" : "出库";
		cpxxSet.setKeyColumns(new String[] {});
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(391, 260);
		shell.setText("产品" + lbmc + "信息");
		GridLayout gl_shell = new GridLayout(1, false);
		gl_shell.marginTop = 5;
		gl_shell.verticalSpacing = 10;
		shell.setLayout(gl_shell);

		Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("产品信息");
		GridLayout gl_group = new GridLayout(5, false);
		gl_group.marginWidth = 25;
		gl_group.horizontalSpacing = 0;
		gl_group.verticalSpacing = 15;
		group.setLayout(gl_group);

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("产品编号：");

		labCpbh = new CLabel(group, SWT.NONE);
		labCpbh.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labCpbh.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		GridData gd_labCpbh = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_labCpbh.widthHint = 80;
		labCpbh.setLayoutData(gd_labCpbh);
		new Label(group, SWT.NONE);
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		GridData gd_lblNewLabel_1 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_1.horizontalIndent = 30;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
		lblNewLabel_1.setText("产品名称：");

		labCpmc = new CLabel(group, SWT.NONE);
		labCpmc.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labCpmc.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labCpmc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("产品规格：");

		labCpgg = new CLabel(group, SWT.NONE);
		labCpgg.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labCpgg.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labCpgg.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		labLb = new CLabel(group, SWT.NONE);
		labLb.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labLb.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labLb.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));
		labLb.setText("");

		Label label_1 = new Label(group, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("入 库 价：");

		labCbj = new CLabel(group, SWT.NONE);
		labCbj.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		labCbj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		labCbj.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		labCbj.setText("上一次该产品入库的价格");
		Group group_1 = new Group(shell, SWT.NONE);
		GridLayout gl_group_1 = new GridLayout(4, false);
		gl_group_1.marginWidth = 25;
		gl_group_1.marginHeight = 10;
		gl_group_1.horizontalSpacing = 0;
		gl_group_1.verticalSpacing = 15;
		group_1.setLayout(gl_group_1);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_1.setText("" + lbmc + "信息");

		Label lblNewLabel_6 = new Label(group_1, SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_6.setText("" + lbmc + "单价：");

		textdj = new Text(group_1, SWT.BORDER);
		textdj.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				textdj.selectAll();
			}
		});
		textdj.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textdj.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_textdj = new GridData(SWT.FILL, SWT.FILL, false, false, 1,
				1);
		gd_textdj.widthHint = 80;
		textdj.setLayoutData(gd_textdj);
		ControlUtil.setCurrencyRule(textdj, false, false);
		ControlUtil.setEnterToFocusMove(textdj);

		Label lblNewLabel_3 = new Label(group_1, SWT.NONE);
		GridData gd_lblNewLabel_3 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_3.horizontalIndent = 30;
		lblNewLabel_3.setLayoutData(gd_lblNewLabel_3);
		lblNewLabel_3.setText("" + lbmc + "数量：");

		textSl = new Spinner(group_1, SWT.BORDER);
		textSl.setSelection(1);
		GridData gd_textSl = new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1);
		gd_textSl.widthHint = 30;
		textSl.setLayoutData(gd_textSl);

		ControlUtil.setEnterToFocusMove(textSl);

		Label lblNewLabel_5 = new Label(group_1, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_5.setText("备    注：");

		textBz = new Text(group_1, SWT.BORDER);
		textBz.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 3,
				1));
		ControlUtil.setEnterToFocusMove(textBz);
		Composite composite = new Composite(shell, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.spacing = 20;
		composite.setLayout(rl_composite);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkpressed();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("确定");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setText("退出");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				fwxm = null;
				shell.close();
			}
		});
		setParams();
		setValues();
		if (kcvo != null) {
			textdj.setText(kcvo.dj != null ? kcvo.dj : "");
			textSl.setSelection(kcvo.sl > 0 ? kcvo.sl : 0);
			textBz.setText(kcvo.bz != null ? kcvo.bz : "");
		}
	}

	protected void doOkpressed() {
		if (lb == 2
				&& textSl.getSelection() > cpxxSet.getValue("kcsl",
						Double.class)) {
			MessageDialog.openInformation(shell, "提示", "库存不足，不允许产生负库存");
			textSl.setFocus();
			return;
		}
		kcvo = new CpkcVo();
		kcvo.cpbh = labCpbh.getText();
		kcvo.cpmc = labCpmc.getText();
		kcvo.dj = textdj.getText();
		kcvo.sl = textSl.getSelection();
		kcvo.bz = textBz.getText();
		kcvo.lb = lb;
		result = SWT.OK;
		shell.close();
	}

	protected void setParams() {
	}

	protected void setValues() {
		labCpbh.setText(cpxxSet.getValue("cpbh", String.class, ""));
		labCpmc.setText(cpxxSet.getValue("cpmc", String.class, ""));
		labCpgg.setText(cpxxSet.getValue("gg", String.class, ""));
		labCbj.setText(cpxxSet.getValue("zjjj", String.class, ""));
		textdj.setText(cpxxSet.getValue("zjjj", String.class, ""));
	}
}
